/**
 * 文件名称:  BaseCarController.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2017/2/27 14:16
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.crm.base.controller;


import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.base.service.IBaseCarService;
import com.hongdee.crm.car.entity.TCrmCar;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 名  称:BaseCarController
 * 类  型: class
 * 描  述:  基盘查询控制类
 * 路  径:com.hongdee.crm.base.controller
 * 作  者:zhangkai
 * 日  期:2017/2/27 14:16
 */
@RequestMapping("/car/base")
@RestController
public class BaseCarController {
    @Autowired
    private IBaseCarService baseCarService;

    /**
     * 分页查询基盘车辆
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "uid", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map params) throws Exception {

        String companyId = TokenUtil.getCompanyId(uid);
        Map pageMap = baseCarService.page(start, limit, params, uid);
        pageMap.put("success", true);
        pageMap.put("msg", "基盘分页成功！");
        return pageMap;
    }

    /**
     * 区间客户基盘数
     *
     * @param uid
     * @param token
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/baseCount")
    @ResponseBody
    public Map baseCount(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "uid", defaultValue = "") String token,
                         @RequestParam(name = "endDate", defaultValue = "") String endDate,
                         @RequestParam(name = "SA", defaultValue = "") String SA,
                         @RequestParam(name = "companyId",defaultValue = "")String companyId) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.baseCount(endDate, uid, SA,companyId);
        pageMap.put("success", true);
        pageMap.put("msg", " 区间基盘数查询成功！");
        return pageMap;
    }

    /**
     * 区间客户基增长数量
     *
     * @param uid
     * @param token
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/growCount")
    @ResponseBody
    public Map growCount(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "uid", defaultValue = "") String token,
                         @RequestParam(name = "endDate", defaultValue = "") String endDate,
                         @RequestParam(name = "SA", defaultValue = "") String SA,
                         @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.baseGrowCount(endDate, uid, SA,companyId);
        pageMap.put("success", true);
        pageMap.put("msg", "区间增长数量查询成功！");
        return pageMap;
    }

    /**
     * 区间客户基增第率
     *
     * @param uid
     * @param token
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/growLv")
    @ResponseBody
    public Map growLv(@RequestParam(name = "uid", defaultValue = "") String uid,
                      @RequestParam(name = "uid", defaultValue = "") String token,
                      @RequestParam(name = "endDate", defaultValue = "") String endDate,
                      @RequestParam(name = "SA", defaultValue = "") String SA,
                      @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.baseGrowLv(endDate, uid, SA,companyId);
        pageMap.put("success", true);
        pageMap.put("msg", "区间增长率查询成功！");
        return pageMap;
    }

    /**
     * 区间客户流失数量
     *
     * @param uid
     * @param token
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/lostCount")
    @ResponseBody
    public Map lostCount(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "uid", defaultValue = "") String token,
                         @RequestParam(name = "endDate", defaultValue = "") String endDate,
                         @RequestParam(name = "SA", defaultValue = "") String SA,
                         @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.lostCount(endDate, uid, SA,companyId);
        pageMap.put("success", true);
        pageMap.put("msg", "区间流失数量查询成功！");
        return pageMap;
    }

    /**
     * @Name: 区间客户数量流失详情
     * @Author: Liu XiaoLong
     * @Date: 2017/9/18 15:02
     * @Remark:
     */
    @RequestMapping("/lostCountDetail")
    @ResponseBody
    public Map lostCountDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "uid", defaultValue = "") String token,
                         @RequestParam(name = "endDate", defaultValue = "") String endDate,
                         @RequestParam(name = "SA", defaultValue = "") String SA,
                         @RequestParam(name = "companyId", defaultValue = "") String companyId,
                         @RequestParam(name = "start",defaultValue = "0") String start,
                         @RequestParam(name = "limit",defaultValue = "50") String limit) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.lostCountDetail(endDate, uid, SA,companyId,start,limit);
        pageMap.put("success", true);
        pageMap.put("msg", "区间流失数量查询成功！");
        return pageMap;
    }

    /**
     * 区间客户流失率
     *
     * @param uid
     * @param token
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/lostLv")
    @ResponseBody
    public Map lostLv(@RequestParam(name = "uid", defaultValue = "") String uid,
                      @RequestParam(name = "uid", defaultValue = "") String token,
                      @RequestParam(name = "endDate", defaultValue = "") String endDate,
                      @RequestParam(name = "SA", defaultValue = "") String SA,
                      @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.lostLv(endDate, uid, SA,companyId);
        pageMap.put("success", true);
        pageMap.put("msg", "区间流失率查询成功！");
        return pageMap;
    }

    /**
     * 区间客户活跃度
     *
     * @param uid
     * @param token
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping("/activeLv")
    @ResponseBody
    public Map activeLv(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "uid", defaultValue = "") String token,
                        @RequestParam(name = "endDate", defaultValue = "") String endDate,
                        @RequestParam(name = "SA", defaultValue = "") String SA,
                        @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        if (StringUtils.isEmpty(endDate)) {
            throw new Exception("请选择基盘区间");
        }
        Map pageMap = baseCarService.activeLv(endDate, uid, SA,companyId);
        pageMap.put("success", true);
        pageMap.put("msg", "活跃度查询成功！");
        return pageMap;
    }


    /**
     * @return List
     * @throws Exception
     * @desc 行政区域下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/areaCombo")
    @ResponseBody
    public List<Map> areaCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        return this.baseCarService.areaCombo(uid,companyId);
    }

    /**
     * 基盘数据导出
     **/
    @RequestMapping("/exportExecl")
    @ResponseBody
    public ResultEntity exportExecl(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    HttpServletRequest request, HttpServletResponse response,
                                    @RequestBody Map params) throws Exception {
        List dataList = this.baseCarService.listExport(params, uid);
        XSSFWorkbook wb = this.baseCarService.createExcel(dataList, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
         path =   path + File.separator + "upload";

        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

    @RequestMapping("/add/car")
    @ResponseBody
    public ResultEntity addCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestBody TCrmCar crmCar){
        return null;
    }
}
